﻿using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.BLL.WxInterfaceFunc;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Attributes;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class LoginController : BaseController
    {
        private readonly MatrixCloudCenterInterFuncBll _matrixCloudCenterInterFuncBllobj = new MatrixCloudCenterInterFuncBll();
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public ActionResult Index()
        {
            var menucookie = new HttpCookie("UserNavMenuCookie") { Expires = DateTime.Now.AddDays(-1) };
            Response.Cookies.Add(menucookie);
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult LoginValidate(string userName, string pwd)
        {
            var operatorId = 0;
            var result = new Result();
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(pwd) || userName.Length > 32 || pwd.Length > 32)
            {
                result.Status = false;
                result.Message = "请正确输入";
            }
            else
            {
                var model = MagUserBLL.Instance.GetModel(userName.Trim(), EncryptionLib.Encrypt(pwd, Encoding.UTF8));
                if (model == null)
                {
                    result.Status = false;
                    result.Message = "账号或者密码错误";
                }
                else
                {
                    Session["user"] = model;
                    var userCookie = new HttpCookie("user");
                    userCookie.Values["u"] = EncryptionLib.Encrypt(model.UserName, Encoding.UTF8);
                    userCookie.Values["p"] = model.Pwd;
                    Response.Cookies.Add(userCookie);
                    model.LastLoginIp = GetIP();
                    MagUserBLL.Instance.Update(model);
                    result.Status = true;
                    operatorId = model.Id;
                }
            }
            MagUserOperatorLogBLL.Instance.Add(new MagUserOperatorLog
            {
                AddTime = DateTime.Now,
                ActionDescription =
                    "[管理员登录：" + (result.Status ? "成功" : "失败") + "]：账号：" + userName + "、密码：" +
                    (result.Status ? EncryptionLib.Encrypt(pwd, Encoding.UTF8) : pwd),
                ActionIp = GetIP(),
                OperaterId = operatorId
            });
            return Json(result);
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Logout()
        {
            Session["userModules" + CurrentUser.Id] = null;
            Session["user"] = null;
            var newcookie = new HttpCookie("user") { Expires = DateTime.Now.AddDays(-1) };
            Response.Cookies.Add(newcookie);

            var menucookie = new HttpCookie("UserNavMenuCookie") { Expires = DateTime.Now.AddDays(-1) };
            Response.Cookies.Add(menucookie);

            return Json(true);
        }
    }
}
